//microcommand define pptreval
let page
ctx.inject(["puppeteer"], ctx => {
  ctx.on("ready", async () => {
    page = await ctx.puppeteer.page()
  })
  ctx.on("dispose", () => {
    page?.close().catch(() => {})
  })
})
signature("<code:text>", { authority: 3, strictOptions: true })
before((_, code) => {
  if (!code) throw new SessionError(["internal.insufficient-arguments"])
})
action(async (_, code) => {
  if (!page) return
  try {
    const result = await page.evaluate(code)
    return h.text(typeof result === "string" ? result : require("util").inspect(result))
  } catch (err) {
    return h.text(String(err))
  }
})
locale("zh-CN", { description: "用浏览器运行 JavaScript 代码" })